package deployment.generater;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * MyBatis Plus 代码生成器
 */
public class MyBatisPlusGenerator {
    // 数据库连接信息常量
    private static final String URL = "jdbc:mysql://localhost:3306/health?serverTimezone=GMT%2B8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    // 包配置常量
    private static final String PARENT_PACKAGE_TEST = "com.soft.czhms";

    public static void main(String[] args) {
        try {
            FastAutoGenerator generator = FastAutoGenerator.create(URL, USERNAME, PASSWORD);

            // 动态设置输出路径，支持跨平台
            String outputDir = "src" + java.io.File.separator + "main" + java.io.File.separator + "java";

            generator.globalConfig(builder -> {
                        builder.author("叶笑秋")
                                .outputDir(outputDir);
                    })
                    .packageConfig(builder -> {
                        builder.parent(PARENT_PACKAGE_TEST)
                                .entity("pojo.po")
                                .mapper("dao.mapper")
                                .service("service")
                                .serviceImpl("service.impl")
                                .controller("web.controller")
                                .xml("dao.mapperxml");
                    })
                    .strategyConfig(builder -> {
                        builder
                            .addInclude(".*") // 示例：生成所有表
                            .entityBuilder()
                                .enableLombok()
                                .enableTableFieldAnnotation()
                                .idType(IdType.AUTO)
                            .serviceBuilder()
                                .formatServiceFileName("%sService")
                            .mapperBuilder()
                            .controllerBuilder()
                            .build();
                    })
                    .templateEngine(new FreemarkerTemplateEngine())
                    .execute();

        } catch (Exception e) {
            System.err.println("代码生成失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
}
